package lia.org.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lia.org.model.Marca;
import lia.org.model.Modelo;
import lia.org.service.ModeloService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/modelo")
public class ModeloController extends CommonControl<Modelo> {

	@Autowired
	private ModeloService modeloService;

	@RequestMapping(method = RequestMethod.GET, value = "/get/{name}", produces = "application/json")
	public @ResponseBody
	Modelo get(@PathVariable("name") String name) {
		return modeloService.getById(name);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/delete/{name}", produces = "application/json")
	public @ResponseBody
	Map<String, ? extends Object> deleteById(@PathVariable("name") String name) {
		try {
			modeloService.deleteById(modeloService.getById(name));
			return getModelMapSucces("OK");
		} catch (Exception e) {
			return getModelMapError(e.getMessage());
		}

	}

	@RequestMapping(method = RequestMethod.GET, value = "/list", produces = "application/json")
	public @ResponseBody
	List<Modelo> list() {
		List<Modelo> resultado = new ArrayList<Modelo>(0);
		resultado.add(new Modelo("Todos"));
		resultado.addAll( modeloService.findAll(null));

		return resultado;
	}

	
	@RequestMapping(method = RequestMethod.POST, value = "/create", produces = "application/json")
	public @ResponseBody
	Map<String, ? extends Object> create(
			@RequestParam(value = "name", required = true) String name) {
		try {
			modeloService.add(new Modelo(name));
			return getModelMapSucces("OK");
		} catch (Exception e) {
			return getModelMapError(e.getMessage());
		}

	}

}
